package com.xgry.bpm.api.base.menu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgry.bpm.api.base.menu.dao.TBMenuDao;
import com.xgry.bpm.api.base.menu.entity.TBMenu;
import com.xgry.bpm.api.base.menu.form.TBMenuForm;
import com.xgry.bpm.api.base.menu.service.TBMenuService;
import com.xgry.bpm.api.base.menu.vo.TBMenuVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * TBMenu服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-14
 */
@Service
        public class TBMenuServiceImpl extends ServiceImpl<TBMenuDao, TBMenu>implements TBMenuService {

        @Override
        public int save(TBMenuForm record){
    TBMenu data=this.convertDO(record);
        return baseMapper.insert(data);
        }

        @Override
        public int updateById(TBMenuForm record){
        //修改时间
    TBMenu data=this.convertDO(record);
        return baseMapper.updateById(data);
        }

        @Override
        public int deleteById(String id){
        return baseMapper.deleteById(id);
        }

        @Override
        public TBMenuVO selectById(String id){
        TBMenu data=baseMapper.selectById(id);
        TBMenuVO vo=new TBMenuVO();
        BeanUtils.copyProperties(data,vo);
        return vo;
        }

        @Override
        public List<TBMenuVO>selectAll(TBMenuForm record){
    TBMenu data=this.convertDO(record);
        QueryWrapper<TBMenu>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        List<TBMenu>list=baseMapper.selectList(queryWrapper);
        return this.convert(list);
        }

        @Override
        public IPage<TBMenuVO>selectPage(TBMenuForm record){
        // form -> do 转换
    TBMenu data=this.convertDO(record);
        // 分页数据设置
        Page<TBMenu>page=new Page<>(record.getCurrent(),record.getSize());
        // 查询条件
        QueryWrapper<TBMenu>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        IPage<TBMenu>list=baseMapper.selectPage(page,queryWrapper);
        IPage<TBMenuVO>iPage=new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
        }

        @Override
        public IPage<TBMenu>getPage(IPage<TBMenu>page,QueryWrapper<TBMenu>queryWrapper){
        return baseMapper.selectPage(page,queryWrapper);
        }

    @Override
    public TBMenu findMenuByName(String modulerName) {
        return baseMapper.selectOne(new QueryWrapper<TBMenu>(){{
            this.eq("v_menu_name",modulerName);
        }});
    }

    @Override
    public TBMenu findMenuByCode(String modulerCode) {
        return baseMapper.selectOne(new QueryWrapper<TBMenu>(){{
            this.eq("v_menu_code",modulerCode);
        }});
    }

    @Override
    public TBMenu moveUpMenu(Integer MenuId) {
        return updateModulerSortUpOrDown(MenuId,true);
    }

    @Override
    public TBMenu moveDownMenu(Integer MenuId) {
        return updateModulerSortUpOrDown(MenuId,false);
    }
    private TBMenu updateModulerSortUpOrDown(Integer MenuId, Boolean isUp){
        TBMenu tbMenu = baseMapper.selectById(MenuId);
        List<TBMenu> tbMenuers = baseMapper.selectList(new QueryWrapper<TBMenu>() {{
            if (isUp) {//v_menu_code
                this.le("i_menu_sort", tbMenu.getIMenuSort());
                this.orderByDesc("i_menu_sort");
            }else{
                this.ge("i_menu_sort", tbMenu.getIMenuSort());
                this.orderByAsc("i_menu_sort");
            }
        }});

        if (CollectionUtils.isEmpty(tbMenuers)) {
            return null;
        }

        //交换排序号
        TBMenu proMenuer = tbMenuers.get(0);
        Integer iMenuSort = proMenuer.getIMenuSort();
        proMenuer.setIMenuSort(tbMenu.getIMenuSort());
        tbMenu.setIMenuSort(iMenuSort);
        baseMapper.updateById(tbMenu);
        baseMapper.updateById(proMenuer);
        return tbMenu;
    }


    /**
        * Form -> Do
        * @param form 对象
        * @return Do对象
        */
        private TBMenu convertDO(TBMenuForm form){
            TBMenu data=new TBMenu();
            BeanUtils.copyProperties(form,data);
        return data;
        }

        /**
        * Do -> VO
        * @param list 对象
        * @return VO对象
        */
        private List<TBMenuVO>convert(List<TBMenu>list){
        List<TBMenuVO> tBMenuList=new ArrayList<>();
        if(CollectionUtils.isEmpty(list)){
        return tBMenuList;
        }
        for(TBMenu source:list){
    TBMenuVO target=new TBMenuVO();
        BeanUtils.copyProperties(source,target);
    tBMenuList.add(target);
        }
        return tBMenuList;
        }

        }
